package cn.itcast.bos.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.dao.base.CourierDao;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.crm.domain.Customer;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class FixedAreaAction extends ActionSupport implements
		ModelDriven<FixedArea> {
	// 注入业务层
	@Autowired
	private FixedAreaService fs;
	private FixedArea fa = new FixedArea();

	public void setFa(FixedArea fa) {
		this.fa = fa;
	}

	@Override
	public FixedArea getModel() {
		// TODO Auto-generated method stub
		return fa;
	}

	@Action(value = "fixedArea_save", results = { @Result(name = "success", location = "./pages/base/fixed_area.html", type = "redirect") })
	public String save() {
		fs.save(fa);
		return SUCCESS;
	}

	// 接收页面数据
	private Integer page;
	private Integer rows;

	public void setPage(Integer page) {
		this.page = page;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	// 条件查询
	@Action(value = "fixedArea_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		Pageable pb = new PageRequest(page - 1, rows);
		Specification<FixedArea> sf = new Specification<FixedArea>() {

			@Override
			public Predicate toPredicate(Root<FixedArea> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				if (StringUtils.isNotBlank(fa.getId())) {
					Predicate p1 = cb.equal(root.get("id").as(String.class),
							fa.getId());
					list.add(p1);
				}
				if (StringUtils.isNotBlank(fa.getCompany())) {
					Predicate p2 = cb.like(
							root.get("company").as(String.class),
							"%" + fa.getCompany() + "%");
					list.add(p2);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		// 调用业务层获取pageData
		Page<FixedArea> pageData = fs.findPageData(sf, pb);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", pageData.getTotalElements());
		map.put("rows", pageData.getContent());
		// 将获得的数据压入栈顶
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}

	// 查询未关联的人员信息
	@Action(value = "noFixedAreaId", results = { @Result(name = "success", type = "json") })
	public String noFixedAreaId() {
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9999/crm_management/services/customerService/noFixedAreaId")
				.accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;

	}

	// 查询已经关联的人员信息
	@Action(value = "hasFixedAreaId", results = { @Result(name = "success", type = "json") })
	public String hasFixedAreaId() {
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9999/crm_management/services/customerService/hasFixedAreaId")
				.type(MediaType.APPLICATION_JSON)
				.accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}

	private Integer courierId;
	private Integer takeTimeId;

	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	// 快递员关联到定区
	@Action(value = "associationCourierToFixedArea", results = { @Result(name = "success", location = "./pages/base/fixed_area.html") })
	public String toFixedArea() {
		// 调用业务层的方法快递员关联定区
		fs.toFixedArea(fa, courierId, takeTimeId);

		return SUCCESS;

	}
}
